package com.czmh.etc.onlinepubsys.jy.utils;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class SHAUtil {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(SHAUtil.class);


    /***
     *  SHA-256签名
     * @param str 需要加密的内容
     * @return signStr 签名字符串
     */
    public static String getSHA256Sign(String str) {
        return getSign(str, "SHA-256");
    }

    public static String getSign(String str, String methodName) {
        MessageDigest messageDigest;
        String signStr = "";
        try {
            messageDigest = MessageDigest.getInstance(methodName);
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            signStr = Base64.encodeBase64String(hash);
        } catch (NoSuchAlgorithmException e) {

            LOGGER.error("NoSuchAlgorithmException", e);

        } catch (UnsupportedEncodingException e) {
            LOGGER.error("UnsupportedEncodingException", e);

        }
        return signStr;
    }

    private SHAUtil() {

    }
}